import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  //这是什么意思？
  final GlobalKey<_AnimationDemo01State> demo01Key = GlobalKey();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
      appBar: AppBar(
        title: Text("动画测试"),
      ),
      body: AnimationDemo01(
        key: demo01Key,
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.play_circle_filled),
        onPressed: () {
          if (!demo01Key.currentState.animationController.isAnimating) {
            demo01Key.currentState.animationController.forward();
          } else {
            demo01Key.currentState.animationController.stop();
          }
        },
      ),
    ),
    );
  }
}

class AnimationDemo01 extends StatefulWidget {
  AnimationDemo01({Key key}) : super(key: key);

  @override
  _AnimationDemo01State createState() => _AnimationDemo01State();
}

class _AnimationDemo01State extends State<AnimationDemo01>
    with SingleTickerProviderStateMixin {
  AnimationController animationController;

  Animation<double> animation;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    //1、创建AnimationController
    animationController =
        AnimationController(duration: Duration(seconds: 1), vsync: this);

    //2、动画添加Curve效果
    animation = CurvedAnimation(
        parent: animationController,
        curve: Curves.elasticInOut,
        reverseCurve: Curves.easeOut);

    //3、监听动画
    animation.addListener(() {
      setState(() {});
    });

    //4、控制动画的翻转
    animation.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        animationController.reverse();
      } else if (status == AnimationStatus.dismissed) {
        animationController.forward();
      }
    });

    //5、设置值得范围
    animation = Tween(begin: 50.0, end: 120.0).animate(animationController);
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Icon(
        Icons.favorite,
        color: Colors.red,
        size: animation.value,
      ),
    );
  }

  @override
  void dispose() {
    animationController.dispose();
    super.dispose();
  }
}
